package com.cnfangmao.masterdata.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.cnfangmao.masterdata.model.RolePermission;
import com.cnfangmao.masterdata.dao.RolePermissionDao;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.RolePermissionDto;
import com.cnfangmao.masterdata.dto.RolePermissionSearchDto;
import com.cnfangmao.masterdata.service.RolePermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色权限关系表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionDao, RolePermission> implements RolePermissionService {

	@Autowired
	private RolePermissionDao dao;
	
	@Override
	public RolePermission findByRoleIdAndPermissionId(String roleId,
			String permissionId) {
		RolePermission rolePermission =null;
		QueryWrapper<RolePermission> queryWrapper =new QueryWrapper<RolePermission>();
		queryWrapper.eq("role_id", roleId);
		queryWrapper.eq("permission_id", permissionId);
		rolePermission=dao.selectOne(queryWrapper);
		return rolePermission;
	}

	@Override
	public IPage<RolePermission> queryListRolePermission(
			GeneralSearchDto<RolePermissionSearchDto> dto) {
		IPage<RolePermission> page = new Page<RolePermission>(dto.getPageNo(), dto.getPageSize());
		IPage<RolePermission> listPage =dao.queryListRolePermission(page,dto.getConditions().getRoleName(), dto.getConditions().getPermissionName());
		return listPage;
	}

	@Override
	public List<RolePermissionDto> getRolePermission(String roleId) {
		List<RolePermission> entityList= dao.findRolePermissionByRoleId(roleId);
		List<RolePermissionDto> rolePermissionList = new ArrayList<RolePermissionDto>();
		for (RolePermission entity : entityList) {
			RolePermissionDto dto = new RolePermissionDto();
			dto.setRoleId(entity.getRoleId());
			dto.setPermissionId(entity.getPermissionId());
			rolePermissionList.add(dto);
		}
		 return rolePermissionList;
	}

}
